package com.example.assisteddriving;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.assisteddriving.bean.Tiwenlog;
import com.example.assisteddriving.bean.Xinlvlog;
import com.example.assisteddriving.dao.DrivelogDao;
import com.example.assisteddriving.service.AnalyseSevice;
import com.example.assisteddriving.service.Xinlvservice;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Xinlv extends AppCompatActivity {
    Intent xinlvIntent;
    public static Handler myHandler;
    ImageView back;
    TextView nxl,lxl,axl,ntw,ltw,atw,nxy,lxy,axy;
    private float xl,tw,xy;
    public static final String PREFERENCE_NAME = "gerenxinxi";  //文件名
    public static int MODE = MODE_PRIVATE;  //操作模式
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xinlv);
        initview();
        myHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Bundle data = msg.getData();
                switch (msg.what){
                    case 0x003:
                        nxl.setText(data.getString("xinlv"));
                        Log.v("dsa",data.getString("tiwen"));
                        ntw.setText(data.getString("tiwen"));
                        nxy.setText(data.getString("xueyang"));
                        DrivelogDao drivelogDao=new DrivelogDao(Xinlv.this);
                        List<Xinlvlog> list=drivelogDao.getxl();
                        if (list.size()>1){
                            lxl.setText(list.get(list.size()-2).getXinlv());
                            ltw.setText(list.get(list.size()-2).getTiwen());
                            lxy.setText(list.get(list.size()-2).getXueyang());
                        }else if(list.size()==0){
                            lxl.setText("0");
                            ltw.setText("0");
                            lxy.setText("0");
                        }
                        avg();
                        axl.setText(String.valueOf(xl));
                        atw.setText(String.valueOf(tw));
                        axy.setText(String.valueOf(xy));
                        if (Float.parseFloat(data.getString("xinlv"))>xl&&Float.parseFloat(data.getString("xinlv"))<100){

                        }
                        break;

                }
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        xinlvIntent=new Intent(this, Xinlvservice.class);
        startService(xinlvIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(xinlvIntent);

    }
    private void initview() {
        SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        String phoneNuml = sharedPreferences.getString("jinjidianhua", null);
        if(phoneNuml==null){
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    //标题
                    .setTitle("提示")
                    //内容
                    .setMessage("请前往“个人信息“界面完善个人信息")
                    //图标
                    .setIcon(R.mipmap.ic_launcher)
                    .setPositiveButton("确认", null)
                    .create();
            alertDialog.show();
        }else {
            AlertDialog alertDialog = new AlertDialog.Builder(Xinlv.this)
                    //标题
                    .setTitle("title")
                    //内容
                    .setMessage("请在将手指轻轻按在传感器上")
                    //图标
                    .setIcon(R.mipmap.ic_launcher)
                    .setPositiveButton("确认", null)
                    .create();
            alertDialog.show();
        }

        back=findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        nxl=findViewById(R.id.nxl);
        lxl=findViewById(R.id.lxl);
        axl=findViewById(R.id.axl);

        ntw=findViewById(R.id.ntw);
        ltw=findViewById(R.id.ltw);
        atw=findViewById(R.id.atw);

        nxy=findViewById(R.id.nxy);
        lxy=findViewById(R.id.lxy);
        axy=findViewById(R.id.axy);

        DrivelogDao drivelogDao=new DrivelogDao(Xinlv.this);
        List<Xinlvlog> list=drivelogDao.getxl();
        if (list!=null){
            nxl.setText("0");
            ntw.setText("0");
            nxy.setText("0");
                lxl.setText(list.get(list.size()-1).getXinlv());
                ltw.setText(list.get(list.size()-1).getTiwen());
                lxy.setText(list.get(list.size()-1).getXueyang());
            avg();
            axl.setText(String.valueOf(xl));
            atw.setText(String.valueOf(tw));
            axy.setText(String.valueOf(xy));
        }


    }

    private void avg() {
        List<Xinlvlog> list=new ArrayList<>();
        DrivelogDao drivelogDao=new DrivelogDao(Xinlv.this);
        list=drivelogDao.getxl();
        int A=0,B=0,C=0;
        if (list.size()!=0) {
            for (int i=0;i<list.size();i++){
                float a= Float.parseFloat(list.get(i).getXinlv())*100;
                float b=Float.parseFloat(list.get(i).getTiwen())*100;
                float c=Float.parseFloat(list.get(i).getXueyang())*100;
                A=(int)a+A;
                B=(int)b+B;
                C=(int)c+C;
                xl=A/100;
            }
            A=(int) A/list.size();
            B=(int) B/list.size();
            C=(int) C/list.size();
            xl=A/100;
            tw=B/100;
            xy=C/100;
        }
    }
}
